package com.i2works.smartluolong.manage.controller.admin;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.i2works.smartluolong.manage.entity.admin.AdminUserDO;
import com.i2works.smartluolong.manage.entity.admin.AdminUserQuery;
import com.i2works.smartluolong.manage.entity.admin.AdminUserVO;
import com.i2works.smartluolong.manage.service.admin.IAdminUserService;
import com.i2works.smartluolong.manage.utils.BaseModule;
import com.i2works.smartluolong.utils.common.BaseUtils;
import com.i2works.smartluolong.utils.base.MsgEnum;
import com.i2works.smartluolong.utils.base.URLConstant;
import com.i2works.smartluolong.utils.entity.R;
import com.i2works.smartluolong.utils.entity.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 系统用户
 *
 * @author koal <koal@vip.qq.com>
 * @ClassName: AdminUserController
 * @date 2015年11月14日 下午11:10:31
 */


@Controller
@RequestMapping("/adminUser")
public class AdminUserController extends BaseModule {

    private final IAdminUserService adminUserService;

    @Autowired
    public AdminUserController(IAdminUserService adminUserService) {
        this.adminUserService = adminUserService;
    }

    /**
     * 系统用户管理页面
     */
    @GetMapping(value = "/view/index")
    public String index(Model model) {
        logger.info("系统用户管理页面");
        return URLConstant.Admin.USER_URL;
    }

    /**
     * 查询系统用户列表
     */

    @GetMapping(value = "/view/api")
    @ResponseBody
    public Result queryList(@ModelAttribute AdminUserQuery query) {
        Result result = new Result();
        try {
            int total = adminUserService.queryCount(query);
            List<AdminUserVO> voList = Lists.newArrayList();
            if (total > 0) {
                List<AdminUserDO> doList = adminUserService.queryList(query);
                voList = BaseUtils.do2voList(doList, AdminUserVO.class);
            }
            return R.success(voList, total);
        } catch (Exception e) {
            logger.error("", e);
            return R.error(MsgEnum.SYSTEM_ERROR.getMsg());
        }
    }


    /**
     * 编辑新增页面跳转
     */
    @GetMapping(value = "/save/form")
    public String savePage() {
        logger.info("用户新增编辑");
        return URLConstant.Admin.USER_FORM_URL;
    }


    /**
     * 添加、编辑系统用户
     */
    @PostMapping(value = "/save/action")
    @ResponseBody
    public Result save(@ModelAttribute AdminUserDO adminUser) {
        Result result = new Result();
        try {
            adminUser.setUpdateName(getUserName());
            adminUserService.save(adminUser);
        } catch (Exception e) {
            logger.error("", e);
            return R.error(MsgEnum.SAVE_ERROR.getMsg());
        }
        return R.success();
    }


    /**
     * 修改密码
     */
    @RequestMapping(value = "/password/form")
    public String passwordPage() {
        logger.info("用户修改密码");
        return URLConstant.Admin.USER_PASS_URL;
    }

    /**
     * 修改密码
     */
    @RequestMapping(value = "/password/action", method = RequestMethod.POST)
    @ResponseBody
    public Result updatePasswordChange(@ModelAttribute AdminUserDO adminUser) {
        Result result = new Result();
        try {
            adminUser.setUpdateName(getUserName());
            adminUserService.updatePasswordChange(adminUser);
        } catch (Exception e) {
            logger.error("", e);
            result.setSuccess(false);
            result.setMsg(MsgEnum.SAVE_ERROR.getMsg());
        }
        return result;
    }

    /**
     * 更新状态
     */
    @RequestMapping(value = "/status/action", method = RequestMethod.POST)
    @ResponseBody
    public Result updateStatus(@ModelAttribute AdminUserDO adminUser) {
        Result result = new Result();
        try {
            adminUser.setUpdateName(getUserName());
            adminUserService.updateStatus(adminUser);
        } catch (Exception e) {
            logger.error("", e);
            result.setSuccess(false);
            result.setMsg(MsgEnum.STATUS_ERROR.getMsg());
        }
        return result;
    }

    @RequestMapping(value = "/status/batchAction", method = RequestMethod.POST)
    @ResponseBody
    public Result updatBatchStatus(@RequestParam("idsList[]") List<Long> idsList, Integer status) {
        Result result = new Result();
        try {
            if (idsList != null && idsList.size() > 0) {
                Map<String, Object> map = Maps.newHashMap();
                map.put("idsList", idsList);
                map.put("status", status);
                map.put("updateName", getUserName());
                adminUserService.updateBatchStatus(map);
            }
        } catch (Exception e) {
            logger.error("", e);
            result.setSuccess(false);
            result.setMsg(MsgEnum.STATUS_ERROR.getMsg());
        }
        return result;
    }


    /**
     * 查询某个系统用户
     */
    @RequestMapping(value = "/view/{id}")
    @ResponseBody
    public Result query(@PathVariable Integer id) {
        Result result = new Result();
        try {
            AdminUserQuery query = new AdminUserQuery();
            query.setId(id);
            AdminUserDO adminUserDO = adminUserService.query(query);
            //过滤敏感信息开始
            AdminUserVO adminUserVO = new AdminUserVO();
            BeanUtils.copyProperties(adminUserDO, adminUserVO);
            //过滤敏感信息结束
            result.setModel(adminUserVO);
        } catch (Exception e) {
            logger.error("", e);
            result.setSuccess(false);
            result.setMsg(MsgEnum.FIND_ERROR.getMsg());
        }
        return result;
    }


    /**
     * 某个系统用户（账号和昵称）是否存在验证
     */
    @RequestMapping(value = "/view/check", method = RequestMethod.POST)
    @ResponseBody
    public boolean queryCheck(@ModelAttribute AdminUserQuery query) {
        boolean result = false;
        try {
            int count = adminUserService.queryCheck(query);
            if (count <= 0) {
                result = true;
            }
        } catch (Exception e) {
            logger.error("", e);
        }
        return result;
    }

}
